<template>
  <div class="cardShareList">
    <div class='tab-container'>
      <van-tabs
        v-model="activeIndex"
        color="#007AE6"
        :line-width="20"
        title-active-color="#007AE6"
        sticky
        animated
        @click="onChange"
      >
        <van-tab
          v-for="item in tabs"
          :key="item.index"
          :title="item.label"
        ></van-tab>
      </van-tabs>
    </div>
    <div class="tab-box">
      <Tips v-if="list.length"
      :isBottom="false"
      :isTool="false" 
      title="名片分享"
      unit="次"
      :increase="todayCount" 
      :reduce="yesterdayCount" />
      <van-list
        v-model="loading"
        :finished="finished"
        :finished-text="finished && !list.length ? '' : '没有更多了'"
        @load="onLoad"
      >
        <div v-for="(item, index) in list" :key="item.shareUuid">
          <div class="info-box cardShareList" @click="jump(item)">
            <div class="left">
              <p class="time">{{ item.updateTimeStr }}</p>
              <p class="text">{{ item.markedWords }}</p>
            </div>
            <p class="count">
              <span class="dot" :class="item.dynamicCount ? '' : 'is-zero'"></span>
              <span>{{item.dynamicCount || 0}}条动态</span>
              <i class="iconfont icon-arrow-"></i>
            </p>
          </div>
          <NewestDivider v-if="findIndex == index" title="新增名片分享" />
        </div>
      </van-list>
    </div>
    <div
      class="no-data"
      v-if='!this.list.length && finished' >
      <img
        src="@/assets/img/dynamics/dynamicsNull.png"
        alt=""
      >
      <p>暂无名片分享数据，快去分享吧~ </p>
    </div>
  </div>
  </div>
</template>
<script>
import userService from 'SERVICE/userService'
import dynamicsService from 'SERVICE/dynamicsService'
import strFormat from '@/filters/strFormat'
import Tips from '@/components/Dynamics/Tips'
import NewestDivider from '@/components/Dynamics/NewestDivider'

export default {
  data: () => ({
    list: [],
    current: 1,
    size: 10,
    loading: false,
    finished: false,
    scrollTop: 0,
    activeIndex: 0,
    todayCount: 0,
    yesterdayCount: 0,
    activeIndex: 2,
    selectIndex: 2,
    findIndex : 0,
    tabs: [
      { index: 0, label: '文章分享', activeIndex: 3, href: '/dynamics/articleShareList' },
      { index: 1, label: '楼盘分享', activeIndex: 1, href: '/dynamics/houseShareList' },
      { index: 2, label: '名片分享', activeIndex: 2, href: '/dynamics/cardShareList' }
    ],
  }),
  created () {
    this.getPosition()
  },
  mounted() {
    this.$nextTick(() =>{
      this.$el.querySelector(".tab-box").scrollTop = this.scrollTop
    })
  },
  components: {
    Tips,
    NewestDivider
  },
  methods: {
    /**
     * 经纪人资料
     */
    async getAgentInfo () {
      try {
        this.agentInfo = await userService.getQrCodeByToken()
      } catch (error) {
        console.log(error)
      }
    },
    /**
     * 获取动态数量
     */
    async getDynamicShareCountVo () {
      try {
        let params = await dynamicsService.getDynamicShareCountVo({ sourceType: 1 })
        this.todayCount = params.todayCount || 0
        this.yesterdayCount = params.yesterdayCount || 0
      } catch (error) {
        console.log(error)
      }
    },
    /**
     * 查看动态
     */
    jump (item) {
      if (Number(item.dynamicCount)) {
        this.$router.push({ name: 'activityTrack', query: { activeIndex: this.selectIndex, shareUuid: item.shareUuid ,count: item.dynamicCount} })
      }
    },
    /**
     * 上拉加载
     */
    async onLoad () {
      try {
        let result = await dynamicsService.getCardDynamicList(this.current)
        if (this.current === 1) {
          this.list = result.records
        } else {
          this.list = [...this.list, ...result.records]
        }
        this.findIndex = this.list.findIndex(item => !item.ifToday) - 1
        if (this.current >= result.pages) {
          this.finished = true
        } else {
          this.current++
        }
        this.loading = false
      } catch (error) {
        console.log(error)
      }
    },
    onChange (index) {
      let current = this.tabs.find(item => item.index === index)
      this.selectIndex = this.tabs[index].activeIndex
      this.$router.replace(current.href)
      // this.$router.push(current.href)
    },
    /**
     * 获取保存位置
     */
    getPosition () {
      try {
        let data = sessionStorage.getItem('cardShareList')
        if (data) {
          let obj = JSON.parse(data)
          for (const key in obj) {
            this[key] = obj[key]
          }
          sessionStorage.removeItem('cardShareList')
        } else {
          this.getDynamicShareCountVo()
        }
      } catch (error) {
        console.log(error)
      }
    }
  },
  beforeRouteLeave (to, from, next) {
    if (to.path === '/dynamics/activityTrack') {
      this.scrollTop = this.$el.querySelector(".tab-box").scrollTop
      sessionStorage.setItem('cardShareList', JSON.stringify(this.$data))
    }
    next()
  }
}
</script>
<style lang="less" scoped>
.cardShareList {
  background: #f7f9fa;
  .tab-box {
    overflow: scroll;
    height: calc(100% - 44px);
  }

  .info-box {
    width: 100%;
    height: 72px;
    padding: 16px;
    display: flex;
    background: #fff;
    align-content: center;
    justify-content: space-between;
    .left {
      color: #333;
      .time {
        font-size: 12px;
        line-height: 16px;
      }
      .text {
        font-size: 16px;
        line-height: 20px;
        font-weight: bold;
        padding-top: 4px;
      }
    }
    .count {
      color: #405170;
      display: flex;
      align-items: center;
      font-size: 14px;
      .dot {
        display: inline-flex;
        width: 8px;
        height: 8px;
        background: #007AE6;
        border-radius: 50%;
        margin-right: 8px;
      }
      .is-zero {
        background-color: #9ca5b5;
      }
    }
  }

  .no-data {
    width: 100%;
    position: absolute;
    top: 45%;
    left: 50%;
    transform: translate(-50%, -50%);
    font-size: 12px;
    font-family: PingFangSC-Regular;
    font-weight: 400;
    color: rgba(153, 153, 153, 1);
    text-align: center;
    img {
      width: 88px;
      height: 88px;
    }
  }
  .ellipsis {
    overflow: hidden;
    text-overflow: ellipsis;
    -o-text-overflow: ellipsis;
    white-space: nowrap;
  }
}
</style>
